//获取应用实例
const app = getApp();

Page({
  data: {
    secondes: '3'
  },
  onLoad: function(e) {
    var that = this;
    var token = wx.getStorageSync('token'); //获取本地缓存中的token
    if (e.auth_code != undefined) {
    
      wx.setStorageSync("auth_code",e.auth_code); //获取本地缓存中的token
  
      if (e.goods_id != undefined) {
        app.globalData.sharedgoodId = e.goods_id
      } else {
        app.globalData.sharedgoodId = '';
      }
    }
    var url = "";
    var num = this.data.secondes;

    var timer = setInterval(function() {
      num--;
      // that.setData({
      //   secondes:num+"s"
      // })
      if (num <= 0) {
        clearInterval(timer);
        // if (token != "") {
          wx.switchTab({
            url: '/pages/live/home',
          })
        // } else {
          var that = this;
          //查看是否授权
          // wx.getSetting({
          //   success: function(res) {
          //     if (res.authSetting['scope.userInfo']) {
          //       console.log("用户授权了");
          //       var that = this;
          //       wx.redirectTo({
          //         url: '/pages/mine/login',
          //       })
          //     } else {
          //       //用户没有授权
          //       console.log("用户没有授权");
          //       wx.redirectTo({
          //         url: '/pages/mine/login',
          //       })
          //     }
          //   }
          // });

        // }

      }
    }, 1000)
  },

  getOpenId(code) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=UserAccount&a=getWxOpenid',
      data: {
        code: code, //
        type:'2'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        console.log(res.data.code)
        if (res.data.code == 0) {
          app.globalData.openid = res.data.data.unionid
          that.checkRegisterOauth(res.data.data.unionid);
        } else {
          wx.showToast({
            title: '' + res.data.msg, //提示文字
            duration: 2000, //显示时长
            icon: 'none',
            mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
            success: function() {}, //接口调用成功
            fail: function() {}, //接口调用失败的回调函数  
            complete: function() {} //接口调用结束的回调函数  
          })
        }
      },
      fail: function() {

      },
      complete() {}
    });
  },

  //判断第三方社交平台账号是否注册
  checkRegisterOauth(openid) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=UserAccount&a=checkRegisterOauth',
      data: {
        openid: openid,
        type: 'wx',
        platform: 'miniwx'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 0) {
          //已经注册过，绑定了手机号码
          wx.setStorageSync('token', res.data.data.token); //将userIdEnc存入本地缓存
          wx.setStorageSync('uid', res.data.data.uid); //将userIdEnc存入本地缓存
          app.globalData.token = res.data.data.token
          app.globalData.uid = res.data.data.uid,
            wx.switchTab({
              url: '/pages/live/home',
            })
        } else {
          //需要绑定手机号
          wx.redirectTo({
            url: '/pages/mine/register',
          })
        }
      },
      fail: function() {

      },
      complete() {}
    });
  },

});